import { Component, OnInit } from '@angular/core';
import { PageEntity } from '../../../core/domain/page.entity';
import { StatusEnum } from '../../../core/enum/status.enum';
import {PaginationUtils} from '../../../utils/paginationUtils';
import {FilterEnum, FilterTypeEnum} from '../../../core/enum/filter.enum';
import {DoFeedbackService} from './do-feedback.service';
import {StorageUtil} from '../../../utils/storage.util';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {NzMessageService} from 'ng-zorro-antd';
import {PostDown} from '../../../utils/postdown';

@Component({
  selector: 'app-do-feedback',
  templateUrl: './do-feedback.component.html',
  styleUrls: ['./do-feedback.component.scss'],
  providers: [DoFeedbackService, PostDown]
})
export class DoFeedbackComponent implements OnInit {
  widthConfigs = [
    '60px', '180px', '200px', '150px', '150px',
    '150px', '150px', '150px', '150px', '150px',
    '150px', '150px', '200px', '180px'
  ];
  pageInfo: PageEntity = new PageEntity();
  allList = [];
  tabType: any = 'all';
  zydxList = [
    {
      text: '中干'
    },
    {
      text: '老师'
    },
    {
      text: '学生'
    }
  ];
  deptList = [];
  pageIndex = 1;
  currObj = null;
  currType = null; // 0,驳回，1，通过，2，详情
  searchObj: SearObj = new SearObj();
  stateObj = {
    0: '新增',
    5: '修改',
    10: '停用'
  };

  isFeedback = false;
  feedback: FeedbackObj = new FeedbackObj();
  teacherList = [];
  validateForm: FormGroup;

  feedbackResult = false; // 反馈结果
  feedbackResultParam = null;

  constructor(
    private service: DoFeedbackService,
    private fb: FormBuilder,
    private message: NzMessageService,
    private postDown: PostDown,
  ) { }

  ngOnInit() {
    this.initForm();
    this.tabType = 'all';
    this.getDept();
    this.resetSearch();
  }

  initForm() {
    this.validateForm = this.fb.group({
      isExecute: [this.feedback.isExecute, [Validators.required]],
      content: [this.feedback.content, [Validators.required]],
      checkerId: [this.feedback.checkerId, [Validators.required]]
    });
  }

  /*面板切换*/
  tabsChange(e) {
    this.tabType = e;
    this.resetSearch();
  }

  // 部门数据查询
  getDept() {
    this.service.getDept().subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.deptList = res.datas;
      }
    });
  }

  // 分页查询
  getPage() {
    if (this.tabType == 'all') {
      delete this.pageInfo.filters.feedbackState;
    } else {
      this.pageInfo.filters.feedbackState = PaginationUtils.filters(this.tabType, FilterEnum.EQUALS);
    }
    this.pageInfo.multiSortMeta = [
      {
        field: 'gmtCreate',
        order: -1
      }
    ];
    this.service.pageExecute(this.pageInfo).subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.allList = res.datas.data;
        this.pageInfo.totalRecords = res.datas.totalRecords;
      }
    });
  }

  // 第几页
  searchPageIndex(event) {
    this.pageInfo.first = event;
    this.getPage();
  }

  // 每页几条
  searchPageSize(event) {
    this.pageInfo.rows = event;
    this.pageInfo.first = 1;
    this.getPage();
  }

  backthis(e) {
    if (e) {
      this.getPage();
    }
    this.pageIndex = 1;
  }

  clickSearch() {
    for (const o in this.searchObj) {
      if (this.searchObj[o]) {
        if (o == 'publishDeptId') {
          this.pageInfo.filters[o] = PaginationUtils.filters(this.searchObj[o], FilterEnum.EQUALS);
        } else {
          this.pageInfo.filters[o] = PaginationUtils.filters(this.searchObj[o], FilterEnum.CONTAINS);
        }
      } else {
        delete this.pageInfo.filters[o];
      }
    }
    this.pageInfo.first = 1;
    this.getPage();
  }

  resetSearch() {
    this.searchObj = new SearObj();
    this.pageInfo = new PageEntity();
    this.getPage();
  }

  look(data) {
    this.currObj = data;
    this.pageIndex = 2;
  }

  toFeedBack(data) {
    this.currObj = data;
    this.feedback = new FeedbackObj();
    this.initForm();
    this.getCurrentLeaders();
    this.getTeachList();
    this.feedback.executeId = data.id;
    this.isFeedback = true;
  }

  // 部门当前领导数据查询
  getCurrentLeaders() {
    this.service.getCurrentLeader().subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {

      }
    });
  }

  // 教师数据查询
  getTeachList() {
    const urser = StorageUtil.getLocalStorage('user');
    const ft = {
      orgId: {
        matchMode: FilterEnum.EQUALS,
        value: urser.teacher.orgId,
        whereType: FilterTypeEnum.AND
      }
    };
    this.service.getTeachList(ft).subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.teacherList = res.datas;
      }
    });
  }

  checkChange(e) {
    if (e) {
      this.teacherList.forEach(item => {
        if (e == item.id) {
          this.feedback.checkerName = item.name;
        }
      });
    }
  }

  // 执行反馈确认
  saveFeedback() {
    this.service.saveFeed(this.feedback).subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.message.success(res.message);
        this.isFeedback = false;
        this.getPage();
      }
    });
  }

  // 反馈结果
  returnFeedBack(data) {
    this.currObj = data;
    this.service.getCheckFeedBacks({id: data.id}).subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.feedbackResultParam = res.datas;
        this.feedbackResult = true;
      }
    });
  }

  /*附件下载*/
  downFj(data) {
    if (data.sourceId) {
      this.postDown.postDownload('security-server/dietc/sys/source/download', {id: data.sourceId});
    } else {
      this.message.warning('无附件！');
    }
  }

}

class SearObj {
  number?: string;
  name?: string;
  actionTarget?: string;
  publisherName?: string;
  deptName?: string;
}

class FeedbackObj {
  checkerId?: string;
  checkerName?: string;
  content?: string;
  executeId?: string;
  id?: string;
  isExecute?: any;
}
